<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connecting Elements with a Line</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .element {
            width: 100px;
            height: 50px;
            background-color: #3498db;
            color: #fff;
            text-align: center;
            line-height: 50px;
            margin: 10px;
            position: relative;
        }

        .line {
            position: absolute;
            border: 2px solid #3498db;
        }

    </style>
</head>
<body>

<div class="element" id="element1">Element 1</div>
<div class="element" id="element2">Element 2</div>
<div class="line" id="line"></div>

<script>
  document.addEventListener('DOMContentLoaded', function () {
    // 获取元素
    const element1 = document.getElementById('element1');
    const element2 = document.getElementById('element2');
    const line = document.getElementById('line');

    // 获取元素的位置信息
    const element1Rect = element1.getBoundingClientRect();
    const element2Rect = element2.getBoundingClientRect();

    // 设置线的位置和长度
    line.style.left = `${element1Rect.right}px`;
    line.style.top = `${element1Rect.top + element1Rect.height / 2}px`;
    line.style.width = `${element2Rect.left - element1Rect.right}px`;
  });

</script>
</body>
</html>
